package com.mysoft.gfyx.marketdevelop.mip.dto;

import com.mysoft.framework.modeling.dto.Filter;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

abstract public class SearchBaseRequestDto {
    public void addFilterByLoadDataApi(List<Filter> filters) {
        if (filters == null || filters.size() == 0) {
            return;
        }

        filters.forEach(filter -> {
            filter.getCondition().forEach(condition -> {
                setFilterByField(condition.getField(), condition.getOperatorType(), condition.getValue());
            });
        });
    }

    protected abstract void setFilterByField(String field, String operatorType, Object value);

    public void dynamicSetProperty(String field, Object value) {
        Class tClass = this.getClass();
        try {
            PropertyDescriptor pd = new PropertyDescriptor(field, tClass);
            //获得set方法
            Method method = pd.getWriteMethod();
            method.invoke(this, value);
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
            // 吞掉异常，此条件的搜索无效
        }
    }
}
